package action;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import entity.Score;
import entity.Teacher;
import service.ScoreService;
import service.StudentService;
import service.TeacherService;

@Controller
@RequestMapping("/teacher")
public class TeacherAction {

	@Autowired
	private ScoreService scoreService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private TeacherService teacherService;
	@RequestMapping("loginView.do")
	protected String loginView() {
		return "/teacher/login";
	}
	@RequestMapping("login.do")
	protected String login(HttpSession session,Model model,Teacher teacher) {
		if(teacherService.login(teacher, session)!=null) {
			return "/teacher/index";
		}else {
			model.addAttribute("info", "账号密码错误");
			return "/teacher/login";
		}
	}
	@RequestMapping("subject/list.do")
	protected String subjectList(Model model,HttpSession session) {
		model.addAttribute("teacherSubjects", teacherService.teacherSubjects(session));
		return "/teacher/subject_list";
	}
	@RequestMapping("score/addView.do")
	protected String scoreAddView(Model model,String id) {
		model.addAttribute("students", studentService.findByClasses(id));
		model.addAttribute("teacherSubjectId", id);
		return "/teacher/score_add";
		
	}
	@RequestMapping("score/add.do")
	protected String scoreAdd(Model model,String [] studentId,double[] fraction,String teacherSubjectId) {
		model.addAttribute("info",teacherService.addScore(studentId, fraction, teacherSubjectId));
		return "redirect:/teacher/subject/list.do";
	}
	@RequestMapping("score/list.do")
	protected String scoreList(String id,Model model) {
		model.addAttribute("scores",teacherService.findScoresByTeacherSubjcet(id));
		return "/teacher/score_list";
	}
	@RequestMapping("score/alterView.do")
	protected String scoreAlterView(Score score,Model model) {
		model.addAttribute("score",scoreService.findById(score));
		return "/teacher/score_alter";
	}
	@RequestMapping("score/alter.do")
	protected String scoreAlter(String teacherSubjectId,Score score,Model model,HttpSession session) {
		model.addAttribute("info",scoreService.alter(score, session));
		model.addAttribute("id",teacherSubjectId);
		return "redirect:/teacher/score/list.do";
	}
}
